<?php
/* ******************************
	Main aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$error  = '';
	$sum	= 0;
	switch ($action) {
		case 'get_edit_page':
			$req = $_REQUEST['id'];
			$page 			= GetPage($req);
			$data			= array('page'	=> $page);
			
			break;
		case 'get_cartridge_type_list':
			$count = $_REQUEST['count'];
			$hidden = $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			
			$rResult = mysql_query("SELECT 	prod_id,                    				
                    				production.name, count(*)
                    FROM warehouse_detail
                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                    LEFT JOIN a_letter  ON a_letter.id=warehouse.a_letter_id 
                    JOIN production ON production.id=prod_id
                    WHERE warehouse_detail.actived=1 AND 
                    warehouse.id > 6400 AND	
			        (a_letter.`status`<5 OR ISNULL(a_letter_id) )AND			
                    barcode>0 AND
			        ISNULL(warehouse.d_letter_id)
			        GROUP BY production.name");
			
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;			
		case 'get_working_cartridge_list':
			$cartridge_id	= $_REQUEST['id'];
			$count		= $_REQUEST['count'];
			$hidden		= $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			// ა და რ ბარათები წარემოეში დასკანერებიდან წარმოება დასრულებამდე
			if( $cartridge_id == '' ){
				$rResult = mysql_query("SELECT 	warehouse_detail.id,
                    				IFNULL(CONCAT('ა-',a_letter_id) ,CONCAT('R-',warehouse_id)),
                    				production.name, barcode
                    FROM warehouse_detail
                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                    LEFT JOIN a_letter  ON a_letter.id=warehouse.a_letter_id 
                    JOIN production ON production.id=prod_id
                    WHERE warehouse_detail.actived=1 AND 
                    			warehouse.id > 6400 AND			
                    			barcode>0 AND	
				                (a_letter.`status`<5 OR ISNULL(a_letter_id) )AND			
                    			warehouse_detail.`status`=0 AND
			ISNULL(warehouse.d_letter_id)");
				
			}else{				
			    $rResult = mysql_query("SELECT 	warehouse_detail.id,
                    				IFNULL(CONCAT('ა-',a_letter_id) ,CONCAT('R-',warehouse_id)),
                    				production.name, barcode
                    FROM warehouse_detail
                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                    LEFT JOIN a_letter  ON a_letter.id=warehouse.a_letter_id 
                    JOIN production ON production.id=prod_id
                    WHERE warehouse_detail.actived=1 AND 
                    			warehouse.id > 6400 AND			
                    			barcode>0 AND	
				                (a_letter.`status`<5 OR ISNULL(a_letter_id) )AND			
                    			warehouse_detail.`status`=0 AND
			                    ISNULL(warehouse.d_letter_id) and
				                prod_id= '$cartridge_id'");
			}
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;
		case 'get_disworking_cartridge_list':
			$cartridge_id	= $_REQUEST['id'];
			$count		= $_REQUEST['count'];
			$hidden		= $_REQUEST['hidden'];
			
			$data = array(
					"aaData"	=> array()
			);
			// ა და რ ბარათები წარემოება დასრულებიდან ტრანსპორტირების დაწყებამდე
			if( $cartridge_id == '' ){
				$rResult = mysql_query("SELECT 	warehouse_detail.id,
                    				IFNULL(CONCAT('ა-',a_letter_id) ,CONCAT('R-',warehouse_id)),
                    				production.name, barcode
                    FROM warehouse_detail
                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                    LEFT JOIN a_letter  ON a_letter.id=warehouse.a_letter_id
                    JOIN production ON production.id=prod_id
                    WHERE warehouse_detail.actived=1 AND 
                    			warehouse.id > 6400 AND			
                    			barcode>0 AND
				                (a_letter.`status`<5 OR ISNULL(a_letter_id) )AND			
                    			warehouse_detail.`status`=1 AND
			ISNULL(warehouse.d_letter_id)");
				
			}else{				
				$rResult = mysql_query("
				    SELECT 	warehouse_detail.id,
                    				IFNULL(CONCAT('ა-',a_letter_id) ,CONCAT('R-',warehouse_id)),
                    				production.name, barcode
                    FROM warehouse_detail
                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                    LEFT JOIN a_letter  ON a_letter.id=warehouse.a_letter_id
                    JOIN production ON production.id=prod_id
                    WHERE warehouse_detail.actived=1 AND 
                    			warehouse.id > 6400 AND			
                    			barcode>0 AND
				                (a_letter.`status`<5 OR ISNULL(a_letter_id) )AND			
                    			warehouse_detail.`status`=1 AND
			                    ISNULL(warehouse.d_letter_id) and
				                prod_id= '$cartridge_id'");
			}
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
				}
					
				$data['aaData'][] = $row;
			}
			
			break;
    default:
       $error =  "null";
}
	
	
$data['error'] = $error;

echo json_encode( $data );

function GetPage($req){
	$row = mysql_fetch_assoc( mysql_query("	SELECT      client.`name` as `client`,
						production.`name`  as `model`,
						IF(warehouse_detail.`status` =0 , 'დასამუხტია', 'დამუხტულია') as con,
					  `warehouse_detail`.`barcode` as `barcode`
		FROM  `warehouse_detail`
		LEFT JOIN	client ON client.id = SUBSTR(warehouse_detail.barcode, 2,4)
		LEFT JOIN	production ON production.id = SUBSTR(warehouse_detail.barcode, 6,4)
		WHERE		warehouse_detail.id = $req") );
	
	$data = '
		<div id="dialog-form">
		    <fieldset>
		    	<legend>კატრიჯების მიღება</legend>
				<table border="0" width="100%">
				<tr>
					<th>
						<label for="client" style="float: left;">კლიენტი :</label>
					</th>
					<th>
						<div id="client" style="float: left;">'.$row['client'].'</div>
					</th>
				</tr>
				<tr>
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="model">მოდელი :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" id="model">'.$row['model'].'</div>
					</th>
				</tr>
				<tr >
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="condition">მდგომარეობა :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" for="condition">'.$row['con'].'</div>
					</th>
				</tr>
				<tr >
					<th>
						<div  style="margin-top: 12px; float: left;"><label for="barcode">შტრიხკოდი :</label></div>
					</th>
					<th>
						<div  style="margin-top: 12px; float: left;" for="barcode">'.$row['barcode'].'</div>
					</th>
				</tr>								
			</table>
        </fieldset>
		<!-- ID -->
    </div>';
	return 	$data;	
}

?>